package repository;

import java.sql.*;

/**
 * @description:
 * @Author Felix Torn
 * @create: 2023-12-24 19:30
 **/
public class QueryDAO {
    private static final String SELECT_CURRENT_BALANCE = "SELECT amount FROM balances ORDER BY datetime DESC LIMIT 1";

    // 查询当前余额
    public static double getCurrentBalance() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DatabaseConnector.getConnection();
            assert connection != null;
            preparedStatement = connection.prepareStatement(SELECT_CURRENT_BALANCE);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                return resultSet.getDouble("amount");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DatabaseConnector.closeResultSet(resultSet);
            DatabaseConnector.closePreparedStatement(preparedStatement);
            DatabaseConnector.closeConnection(connection);
        }

        return 0.00; // 返回默认值或者抛出异常，具体情况可根据实际需求调整
    }

    public static void main(String[] args) {
        System.out.println(getCurrentBalance());
    }
}
